package com.sqx.modules.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserCertification;
import com.sqx.modules.app.entity.UserMoney;
import com.sqx.modules.app.service.UserCertificationService;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.app.service.UserMoneyService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@AllArgsConstructor
@RequestMapping("/userCertification")
@Api("实 名 认 证")
public class UserCertificationController {
    private UserCertificationService userCertificationService;
    private UserMoneyService userMoneyService;
    private CommonInfoService commonInfoService;


    /**
     * 实名列表
     */
    @RequestMapping("/queryCertification")
    @ApiOperation("实名列表")
    public Result queryCertification(Long page, Long limit, @ApiParam("状态") String status,
                                     @ApiParam("姓名") String name, Integer classify,
                                     Integer isRecommend,Integer onLine,String city,Integer classifyId,
                                     String longitude, String latitude,String shopName,String phone) {
        return userCertificationService.queryCertification(page, limit, status, name, classify,isRecommend,
                onLine,city,classifyId,longitude,latitude,shopName,phone,2);
    }

    @PostMapping("/updateCertification")
    @ApiOperation("修改师傅信息")
    public Result updateCertification(@RequestBody UserCertification userCertification){
        if(userCertification.getOnLine()!=null && userCertification.getOnLine()==1){
            UserCertification oldUserCertification = userCertificationService.getById(userCertification.getId());
            UserMoney userMoney = userMoneyService.getOne(new QueryWrapper<UserMoney>().eq("user_id", oldUserCertification.getUserId()));
            CommonInfo commonInfo = commonInfoService.findOne(616);
            if (userMoney.getSafetyMoney().compareTo(new BigDecimal(commonInfo.getValue())) < 0) {
                return Result.error(508,"当前用户未缴纳足额" + commonInfo.getValue() + "元的保证金");
            }
        }
        userCertificationService.updateById(userCertification);
        return Result.success();
    }

    @PostMapping("/deleteCertification")
    @ApiOperation("删除师傅信息")
    public Result deleteCertification(Long id){
        userCertificationService.removeById(id);
        return Result.success();
    }


    @GetMapping("/selectCertificationById")
    @ApiOperation("查询师傅信息")
    public Result selectCertificationById(Long id){
        return Result.success().put("data",userCertificationService.getById(id));
    }

    /**
     * 查看实名认证详情
     *
     * @param userId
     * @return
     */
    @GetMapping("getCertificationInfo")
    public Result getCertificationInfo(Long userId) {
        return Result.success().put("data", userCertificationService.getCertificationInfo(userId));
    }

    /**
     * 查询已经进行实名的用户
     */
    @RequestMapping("/queryUserCertification")
    @ApiOperation("查询已经进行实名的用户")
    public Result queryUserCertification(Long page, Long limit, String name, String phone, Integer classify,String shopName) {
        Page<UserCertification> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return userCertificationService.queryUserCertification(pages, name, phone, classify,shopName);

    }

    /**
     * 审核实名认证
     */
    @PostMapping("/auditorUserCertification")
    @ApiOperation("审核实名认证")
    public Result auditorUserCertification(@RequestBody JSONObject jsonObject) {
        Long status = jsonObject.getLong("status");
        Long id = jsonObject.getLong("id");
        String remek = jsonObject.getString("remek");
        return userCertificationService.auditorUserCertification(status, id, remek);
    }


}
